from kubernetes import client, config

# 加载 Kubernetes 配置文件
config.load_kube_config()

# 创建 Kubernetes Core V1 客户端
v1 = client.CoreV1Api()

# 获取所有命名空间中的 Service 列表
namespace = 'kuboard'  # 如果想要获取所有命名空间的 Service，请将 namespace 设置为 None
service_list = v1.list_namespaced_service(namespace=namespace)
service = v1.read_namespaced_service()
# 打印 Service 列表
print("Service 列表:")
for service in service_list.items:
    # created_time = service.metadata.creation_timestamp
    # name = service.metadata.name
    # type = service.spec.type
    # selector = service.spec.selector.app
    # cluster_ip = service.spec.cluster_ip
    # port = service.spec.ports[0].node_port
    # print(f"名称: {service.metadata.name}, 类型: {service.spec.type}, 选择器: {service.spec.selector}")
    print(f"名称: {service.metadata.name}, 类型: {service.spec.type}, 选择器: {service.spec.selector}, 端口: {service.spec.ports[0].node_port}")